<template>
  <div class="account-container">
    <!-- 用户信息卡片 -->
    <el-card class="user-card">
      <div class="user-panel">
        <div class="user-info-container">
          <div class="avatar-section">
            <el-avatar :src="userInfo.avatar"  :size="80">
              {{ userInfo.name?.charAt(0)  || 'C' }}
            </el-avatar>
            <div class="user-details">
              <div class="user-id">手机号: {{ userInfo.phone  || '---' }}</div>
              <h3 class="user-name">昵称: {{ userInfo.name  || '客户' }}</h3>
            </div>
          </div>
          
          <div class="action-buttons">
            <el-button 
              class="quit-btn"
              type="warning"
              size="small"
              @click="handleLogout"
            >
              退出登录 
            </el-button>
          </div>
        </div>
      </div>
 
      <!-- 个人信息详情 -->
      <el-divider />
      <div class="info-grid">
        <div class="info-item">
          <div class="info-content">
            <div class="info-label">账户状态</div>
            <div class="info-value">
              <el-tag :type="isAuthenticated ? 'success' : 'warning'"  size="small">
                {{ isAuthenticated ? '正常使用' : '游客登录' }}
              </el-tag>
            </div>
          </div>
        </div>
        <div class="info-item">
          <div class="info-content">
            <div class="info-label">注册时间</div>
            <div class="info-value">{{ registrationDate }}</div>
          </div>
        </div>
        <div class="info-item">
          <div class="info-content">
            <div class="info-label">公司</div>
            <div class="info-value" style="font-size: 10px;">{{ userInfo.company  || '未填写' }}</div>
          </div>
        </div>
      </div>
    </el-card>
 
    <!-- 快捷操作卡片 -->
    <el-card class="quick-actions-card">
      <template #header>
        <div class="card-header">
          <el-icon><Operation /></el-icon>
          <span>快捷操作</span>
        </div>
      </template>
      <div class="quick-actions-grid">
        <div class="quick-action" @click="navigateTo('CPurchase')">
          <el-icon :size="24" color="#409EFF"><ShoppingCart /></el-icon>
          <span>采购指南</span>
        </div>
        <div class="quick-action" @click="navigateTo(isAuthenticated ? 'COrderList' : 'CLogin')">
          <el-icon :size="24" color="#67C23A"><Tickets /></el-icon>
          <span>我的订单</span>
        </div>
        <div class="quick-action" @click="navigateTo(isAuthenticated ? 'COrderHistory' : 'CLogin')">
          <el-icon :size="24" color="#E6A23C"><Histogram /></el-icon>
          <span>我的积分</span>
        </div>
      </div>
    </el-card>
 
    <!-- 账户安全卡片 -->
    <el-card class="security-card">
      <template #header>
        <div class="card-header">
          <el-icon><Lock /></el-icon>
          <span>账户安全</span>
        </div>
      </template>
      <div class="security-content">
        <div class="security-item">
          <div class="security-info">
            <el-icon><Iphone /></el-icon>
            <span>手机号</span>
          </div>
          <el-button size="small" round @click="showPhoneDialog">修改</el-button>
        </div>
        <div class="security-item">
          <div class="security-info">
            <el-icon><Key /></el-icon>
            <span>登录密码</span>
          </div>
          <el-button size="small" round @click="showPasswordDialog">修改</el-button>
        </div>
      </div>
    </el-card>
 
    <!-- 修改手机号对话框 -->
    <el-dialog v-model="phoneDialogVisible" title="修改手机号" width="30%">
      <el-form :model="phoneForm" :rules="phoneRules" ref="phoneFormRef">
        <el-form-item label="新手机号" prop="newPhone">
          <el-input v-model="phoneForm.newPhone"  placeholder="请输入新手机号" />
        </el-form-item>
        <el-form-item label="验证码" prop="verifyCode">
          <div class="verify-code-input">
            <el-input v-model="phoneForm.verifyCode"  placeholder="请输入验证码" />
            <el-button type="primary" plain @click="sendVerifyCode" :disabled="countdown > 0">
              {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
            </el-button>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button round @click="phoneDialogVisible = false">取消</el-button>
        <el-button type="primary" round @click="updatePhone">确认修改</el-button>
      </template>
    </el-dialog>
 
    <!-- 修改密码对话框 -->
    <el-dialog v-model="passwordDialogVisible" title="修改密码" width="30%">
      <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="passwordForm.oldPassword"  type="password" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPassword"  type="password" show-password />
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input v-model="passwordForm.confirmPassword"  type="password" show-password />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button round @click="passwordDialogVisible = false">取消</el-button>
        <el-button type="primary" round @click="submitPasswordChange">确认修改</el-button>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Lock, Key, Iphone, Operation,
  ShoppingCart, Tickets, Histogram 
} from '@element-plus/icons-vue'
import authStore from "@/model/authstore"
 
const router = useRouter()
const isAuthenticated=authStore.isAuthenticated;
// 用户信息 
const userInfo = ref({
  name: '',
  phone: '',
  company: '',
  avatar: '',
  address: '',
  register_date: ''
})
 
// 表单对话框控制 
const phoneDialogVisible = ref(false)
const passwordDialogVisible = ref(false)
const countdown = ref(0)
 
// 表单数据 
const phoneForm = ref({
  newPhone: '',
  verifyCode: ''
})
 
const passwordForm = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})
 
// 计算注册时间 
const registrationDate = computed(() => {
  if (!userInfo.value.register_date)  return '未知'
  try {
    const date = new Date(userInfo.value.register_date) 
    return date.toLocaleDateString() 
  } catch {
    return '未知'
  }
})
 
// 表单验证规则 
const phoneRules = {
  newPhone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  verifyCode: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { pattern: /^\d{6}$/, message: '验证码为6位数字', trigger: 'blur' }
  ]
}
 
const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.value.newPassword)  {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
 
// 初始化数据 
onMounted(() => {
  const authData = authStore.get('auth') 
  if (authData && authData.user)  {
    userInfo.value  = {
      name: authData.user.name  || '客户',
      phone: authData.user.phone  || '',
      company: authData.user.company  || '',
      avatar: authData.user.avatar  || '',
      address: authData.user.address  || '',
      register_date: authData.user.register_date  || ''
    }
  }

})

// 导航方法 
const navigateTo = (routeName) => {
  router.push({  name: routeName })
}
 
// 退出登录 
const handleLogout = () => {
  ElMessageBox.confirm(' 确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    authStore.clear() 
    router.push('/clogin') 
    ElMessage.success(' 已退出登录')
  }).catch(() => {
    ElMessage.info(' 已取消退出登录')
  })
}
 
// 显示修改手机号对话框 
const showPhoneDialog = () => {
  phoneForm.value  = { newPhone: '', verifyCode: '' }
  phoneDialogVisible.value  = true 
}
 
// 显示修改密码对话框 
const showPasswordDialog = () => {
  passwordForm.value  = { oldPassword: '', newPassword: '', confirmPassword: '' }
  passwordDialogVisible.value  = true 
}
 
// 发送验证码 
const sendVerifyCode = () => {
  if (!phoneForm.value.newPhone)  {
    ElMessage.warning(' 请输入手机号')
    return 
  }
  
  // 模拟发送验证码 
  countdown.value  = 60 
  const timer = setInterval(() => {
    countdown.value-- 
    if (countdown.value  <= 0) {
      clearInterval(timer)
    }
  }, 1000)
  
  ElMessage.success(' 验证码已发送')
}
 
// 更新手机号 
const updatePhone = () => {
  // 这里应该是API调用 
  ElMessage.success(' 手机号修改成功')
  phoneDialogVisible.value  = false 
  userInfo.value.phone  = phoneForm.value.newPhone  
}
 
// 更新密码 
const submitPasswordChange = () => {
  // 这里应该是API调用 
  ElMessage.success(' 密码修改成功')
  passwordDialogVisible.value  = false 
}
</script>
 
<style scoped>
.account-container {
  padding: 20px;
  max-width: 600px;
  margin: 0 auto;
}
 
.user-card,
.quick-actions-card,
.security-card {
  margin-bottom: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
 
.user-info-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
 
.avatar-section {
  display: flex;
  align-items: center;
  gap: 20px;
}
 
.user-details {
  line-height: 1.6;
}
 
.user-name {
  margin: 0;
  font-size: 18px;
  color: #333;
  font-weight: 500;
}
 
.user-id {
  font-size: 14px;
  color: #666;
}
 
.action-buttons {
  display: flex;
  gap: 10px;
}
 
.quit-btn {
  background-color: #ff9500;
  color: white;
  padding: 8px 16px;
  border: 0;
  box-shadow: 3px 3px 1px 0 rgba(0, 0, 0, 0.08);
}
 
.quit-btn:hover {
  background-color: #824e06d1;
  box-shadow: 3px 3px 1px 0 rgba(0, 0, 0, 0.296);
}
 
.info-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 10px;
}
 
.info-item {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 12px;
}
 
.info-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}
 
.info-label {
  font-size: 12px;
  color: #888;
  margin-bottom: 4px;
}
 
.info-value {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}
 
.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  font-size: 16px;
}
 
.quick-actions-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 15px;
  padding: 10px 0;
}
 
.quick-action {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 15px 0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}
 
.quick-action:hover {
  background-color: #f5f7fa;
  transform: translateY(-2px);
}
 
.quick-action span {
  font-size: 14px;
  color: #555;
}
 
.security-content {
  padding: 8px 0;
}
 
.security-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
}
 
.security-item:last-child {
  border-bottom: none;
}
 
.security-info {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #555;
}
 
.verify-code-input {
  display: flex;
  gap: 10px;
}
 
.verify-code-input .el-button {
  width: 120px;
}
 
.el-divider {
  margin: 16px 0;
}
</style>